<?php
/**
 * Enter description here...
 *
 * @package if
 * @subpackage module.helper.controllers
 * @since 1.0
 */
class IfHelperSetupController extends IHelperController {
	function doIndex() {
		//目录是否可写
		$writeables = array(
			IF_APP_ROOT . IFile::separator() . "cache",
			IF_APP_ROOT . IFile::separator() . "logs",
			IF_COMPILER_DIR
		);
		$viewName = o("@view.default");
		$view = o("@view.{$viewName}");
		if (isset($view["compile_dir"])) {
			$writeables[] = str_replace("%{locale}", ILocale::current()->name(), $view["compile_dir"]);
		}
		
		//asserts
		if (strstr(IF_APP_ROOT, "ipa")) {
			$writeables[] = dirname(IF_APP_ROOT) . IFile::separator() . "assets";
		}
		else {
			$writeables[] = IF_APP_ROOT . IFile::separator() . "public" . IFile::separator() . "asserts";
		}
		
		$this->messages = array();
		foreach ($writeables as $dir) {
			if (!is_dir($dir) || !is_writable($dir)) {
				$this->messages[] = "<span class='error'>目录 {$dir} 不可写</span>";
			}
			else {
				$this->messages[] = "<span class='correct'>目录 {$dir} 可写</span>";
			}
		}
		
		//rewrite是否开启
		if (if_rewrite()) {
			$this->messages[] = "<span class='correct'>支持 Rewrite</span>";
		}
		else {
			$this->messages[] = "<span class='error'>不支持 Rewrite</span>";
		}
		
		//PHP版本
		if (version_compare(PHP_VERSION, "5.1.0") >= 0) {
			$this->messages[] = "<span class='correct'>PHP版本大于5.1.0(v" . PHP_VERSION . ")</span>";
		}
		else {
			$this->messages[] = "<span class='error'>PHP版本低于5.1.0</span>";
		}
		
		//apache
		if (function_exists("apache_get_version") && strstr(apache_get_version(), "Apache")) {
			$this->messages[] = "<span class='correct'>Web Server是Apache(" . apache_get_version() . ")</span>";
		}
		else {
			$this->messages[] = "<span class='error'>建议使用<a href='http://httpd.apache.org' target='_blank'>Apache</a></span>";
		}		
	}
	
	function doDb() {
		$names = array_keys(IDb::driverClasses());
		$this->drivers = array_combine($names, $names);
		
		if ($this->form()->isValid()) {
			$file = IApplication::singleton()->configDir() . "/_db.php";
			$opt = new IConfig($file);
			$opt->set("default", xn("_new_"));
			$opt->write();
			$this->redirect("feedback");
		}
	}
	
	function doFeedback() {
		if ($this->form()->isValid()) {
			$contents = xn();
			unset($contents["UI_FORM_CREDIT"]);
			$http = new IHttpClient("http://if.4kiki.net/feedback.php");
			$http->setBody($contents);
			$http->request(IHttpClient::METHOD_POST);
			$this->redirectUrl(__BASE__);
		}
	}
}

?>